import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lcapp/api/interface/user_api.dart';
import 'package:lcapp/api/request/response_data.dart';
import 'package:lcapp/basic/user_info.dart';
import 'package:lcapp/config/const_config.dart';
import 'package:lcapp/data/media_asset_data_entity.dart';
import 'package:lcapp/model/permission/permission_operate.dart';
import 'package:lcapp/model/toast/toast.dart';
import 'package:lcapp/widget/asr_cache_image.dart';

class MInSmrzPage extends StatefulWidget {
  const MInSmrzPage({Key? key}) : super(key: key);

  @override
  _MInSmrzPageState createState() => _MInSmrzPageState();
}

class _MInSmrzPageState extends State<MInSmrzPage> {

  TextEditingController nameC = TextEditingController();
  TextEditingController idC = TextEditingController();

  MediaAssetDataEntity qmAsset = MediaAssetDataEntity();
  MediaAssetDataEntity fmAsset = MediaAssetDataEntity();
  String idUrl = '';

  getImage({int type = 1}){

    // getAlbumPermission(context, ()async{
    //   List<MediaAssetDataEntity> res = await AsrMediaPicker.pickImg(
    //       showCamera: true,selectCount: 1);
    //   if(res!=null&&res.length>0){
    //     setState(() {
    //       if(type==1){
    //         qmAsset = res[0];
    //         save();
    //       }else{
    //         fmAsset = res[0];
    //       }
    //     });
    //   }
    // });
  }

  save()async{
    // if(nameC.text.isEmpty||idC.text.isEmpty){
    //   showToast('请输入身份证姓名或者身份证号码');
    //   return;
    // }
    FormData data = FormData.fromMap({
      // 'name':nameC.text,
      // 'idcard':idC.text,
      'file': await MultipartFile.fromFile(qmAsset.uri!, filename: 'upload.JPG'),
    });
    showLoading();
    ResponseData res = await UserAPI.editRealInfo(data);
    hideLoading();
    if(res.code==200){
      nameC.text = res.data['name'];
      idC.text = res.data['number'];
      idUrl = res.data['url'];
    }
  }

  sureSubmit()async{
    if(idUrl.isEmpty){
      showToast('请选择身份证正面照片');
      return;
    }
    if(fmAsset.uri==null||fmAsset.uri!.isEmpty){
      showToast('请选择身份证反面照片');
      return;
    }
    if(nameC.text.isEmpty||idC.text.isEmpty){
      showToast('身份证照片识别失败，请重新选择');
      return;
    }
    Map data = {
      'name':nameC.text,
      'number':idC.text,
      'url': idUrl,
    };
    showLoading();
    ResponseData res = await UserAPI.submitRealInfo(data);
    hideLoading();
    if(res.code==200){
      userInfo.realName = nameC.text;
      userInfo.id_card = idC.text;
      userInfo.card=1;
      Navigator.of(context).pop();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: bgColor,
      body:GestureDetector(
        onTap: (){
          FocusScope.of(context).requestFocus(FocusNode());
        },
        child: Container(
          child:  SingleChildScrollView(
            child: Column(
              children: [
                Container(
                  width: 1.sw,
                  height: 400.w,
                  child: Stack(
                    children: [
                      Image.asset('assets/min/min_smrz_top.png',width: 1.sw,height: 400.w,fit: BoxFit.fill,),
                      Positioned(child: GestureDetector(onTap: (){
                        Navigator.of(context).pop();
                      },
                        child: Icon(Icons.arrow_back_ios,color: Colors.white,size: 50.w,),
                      ),left: 30.w,top: 120.w,)
                    ],
                  ),
                ),
                SizedBox(height: 30.w,),
                GestureDetector(
                  onTap: (){
                    FocusScope.of(context).requestFocus(FocusNode());
                    getImage();
                  },
                  child: Container(
                    width: 550.w,
                    height: 320.w,
                    child: Stack(
                      children: [
                        Visibility(child: Image.asset('assets/min/min_smrz_qm.png',width: 550.w,height: 320.w,fit: BoxFit.fill,),visible: qmAsset.uri!=null,),
                        Center(child: ASRCacheImage(image: qmAsset.uri!,fit: BoxFit.fill,width: 540.w,height: 310.w,placeholder: 'assets/min/min_smrz_qm.png',),)
                      ],
                    ),
                  ),
                ),
                SizedBox(height: 30.w,),
                GestureDetector(
                  onTap: (){
                    FocusScope.of(context).requestFocus(FocusNode());
                    getImage(type: 2);
                  },
                  child: Container(
                    width: 550.w,
                    height: 320.w,
                    child: Stack(
                      children: [
                        Visibility(child: Image.asset('assets/min/min_smrz_hm.png',width: 550.w,height: 320.w,fit: BoxFit.fill,),visible: fmAsset.uri!=null,),
                        Center(child: ASRCacheImage(image: fmAsset.uri!,placeholder: 'assets/min/min_smrz_hm.png',fit: BoxFit.fill,width: 540.w,height: 310.w),)
                      ],
                    ),
                  ),
                ),
                SizedBox(height: 50.w,),
                Container(
                  width: 690.w,
                  height: 110.w,
                  padding: EdgeInsets.only(top: 10.w),
                  decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(10.w)
                  ),
                  child: TextField(
                    controller: nameC,
                    textInputAction: TextInputAction.done,
                    enabled: false,
                    maxLines: 1,
                    maxLength: 10,
                    autocorrect: true,
                    autofocus: false,
                    decoration:  InputDecoration(
                      contentPadding: EdgeInsets.only(left: 20.w,),
                      hintText: "识别姓名",
                      hintStyle: TextStyle(
                        color: const Color(0xFFAEAEAE),
                        fontSize: 28.sp,
                        fontWeight: FontWeight.w500,
                      ),
                      counterText: "",
                      border: const OutlineInputBorder(
                        borderSide: BorderSide.none,
                      ),
                      fillColor: Colors.transparent,
                      filled: true,
                    ),
                  ),
                ),
                SizedBox(height: 20.w,),
                Container(
                  width: 690.w,
                  height: 110.w,
                  padding: EdgeInsets.only(top: 10.w),
                  decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(10.w)
                  ),
                  child: TextField(
                    controller: idC,
                    textInputAction: TextInputAction.done,
                    maxLines: 1,
                    maxLength: 20,
                    autocorrect: true,
                    enabled: false,
                    autofocus: false,
                    decoration:  InputDecoration(
                      contentPadding: EdgeInsets.only(left: 20.w,),
                      hintText: "身份证号",
                      hintStyle: TextStyle(
                        color: const Color(0xFFAEAEAE),
                        fontSize: 28.sp,
                        fontWeight: FontWeight.w500,
                      ),
                      counterText: "",
                      border: const OutlineInputBorder(
                        borderSide: BorderSide.none,
                      ),
                      fillColor: Colors.transparent,
                      filled: true,
                    ),
                  ),
                ),
                SizedBox(height: 40.w,),
                GestureDetector(
                  onTap: (){
                    FocusScope.of(context).requestFocus(FocusNode());
                    sureSubmit();
                  },
                  child: Container(
                    width: 690.w,
                    height: 110.w,
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                        color: Color(0xffFA6E28),
                        borderRadius: BorderRadius.circular(10.w)
                    ),
                    child: Text('提交认证',style: TextStyle(
                        color: Colors.white,
                        fontSize: 34.sp,
                        fontWeight: FontWeight.w600
                    ),),
                  ),
                )
              ],
            ),
          ),
        ),
      ),
    );
  }
}
